package packed_widgets

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/widget"
)

type Toggle struct {
	*BaseComponent
	*widget.Button
	toggle   bool
	OnSwitch func(bool)
}

func (tg *Toggle) ToolbarObject() fyne.CanvasObject {
	return tg.Button
}

func NewToggle(trueText, falseText string, trueIcon, falseIcon fyne.Resource) *Toggle {
	tg := &Toggle{
		BaseComponent: NewBaseComponent(),
		Button:        widget.NewButtonWithIcon(trueText, trueIcon, nil),
		toggle:        true,
	}
	tg.Button.OnTapped = func() {
		tg.toggle = !tg.toggle
		if tg.toggle {
			tg.SetText(trueText)
			tg.SetIcon(trueIcon)
		} else {
			tg.SetText(falseText)
			tg.SetIcon(falseIcon)
		}
		tg.Refresh()
		if tg.OnSwitch != nil {
			tg.OnSwitch(tg.toggle)
		}
	}
	tg.ExtendBaseWidget(tg)
	return tg
}
